package com.duobaoyu.dby.supply.biz.client.resource.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duobaoyu.dby.common.core.constant.SecurityConstants;
import com.duobaoyu.dby.common.core.constant.ServiceNameConstants;
import com.duobaoyu.dby.common.core.constant.StringPool;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.resource.admin.feign.RemoteResourceStateMappingAdminService;
import com.duobaoyu.dby.resource.common.admin.to.config.StateMappingTO;
import com.duobaoyu.dby.supply.biz.client.resource.ResourceStateMappingAdminClient;
import com.duobaoyu.dby.supply.biz.client.resource.bean.dto.StateMappingDTO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.vo.StateMappingVO;
import com.duobaoyu.dby.supply.biz.client.resource.converter.ResourceConverter;
import com.duobaoyu.dby.supply.biz.producer.SupplyChainMQProducer;
import com.duobaoyu.dby.supply.biz.util.RemoteResultCheckUtil;
import com.duobaoyu.dby.supply.core.rocketmq.message.ResourceLocalCacheMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName ResourceStateMappingAdminClientImpl
 * @createTime 15:14:00 2023/10/08
 */
@Component
@RequiredArgsConstructor
public class ResourceStateMappingAdminClientImpl implements ResourceStateMappingAdminClient {

    private final ResourceConverter resourceConverter;
    private final SupplyChainMQProducer supplyChainMQProducer;
    private final RemoteResourceStateMappingAdminService remoteResourceStateMappingAdminService;

    @Override
    public boolean addStateMapping(StateMappingDTO stateMapping) {
        R<Boolean> result = remoteResourceStateMappingAdminService.add(resourceConverter.toStateMapping(stateMapping), SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, stateMapping);
        return result.getData();
    }

    @Override
    public boolean updateStateMapping(StateMappingDTO stateMapping) {
        R<Boolean> result = remoteResourceStateMappingAdminService.update(resourceConverter.toStateMapping(stateMapping), SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, stateMapping);
        return result.getData();
    }

    @Override
    public boolean deleteStateMapping(Long stateMappingId) {
        R<Boolean> result = remoteResourceStateMappingAdminService.delete(stateMappingId, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, stateMappingId);
        return result.getData();
    }

    @Override
    public StateMappingVO queryStateMappingDetail(Long stateMappingId) {
        R<StateMappingTO> result = remoteResourceStateMappingAdminService.queryOne(stateMappingId, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, stateMappingId);
        return resourceConverter.toStateMapping(result.getData());
    }

    @Override
    public Page<StateMappingVO> queryStateMappingPage(StateMappingDTO stateMapping) {
        R<Page<StateMappingTO>> result = remoteResourceStateMappingAdminService.page(resourceConverter.toStateMapping(stateMapping), SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, stateMapping);
        return resourceConverter.toStateMappingPage(result.getData());
    }

    @Override
    public boolean initializationRedisCache() {
        R<Boolean> result = remoteResourceStateMappingAdminService.initialization(SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, StringPool.EMPTY);
        return result.getData();
    }

    @Override
    public boolean clearLocalCache() {
        supplyChainMQProducer.sendDeleteLocalCacheMessage(new ResourceLocalCacheMessage());
        return true;
    }
}
